package pen_exam.meiidi_0224_03_28;


import java.util.Scanner;

//最长公共子序列长度
public class Class01_MaxLength {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] str1 = sc.nextLine().split(" ");
        int N = Integer.parseInt(str1[0]);
        int M = Integer.parseInt(str1[1]);
        String s1 = sc.nextLine();
        String s2 = sc.nextLine();
        System.out.println(getMaxLength(s1, s2,N,M));
    }

    private static int getMaxLength(String s1, String s2,int N,int M) {
        char[] str1 = s1.toCharArray();
        char[] str2 = s2.toCharArray();
        int[][] dp = new int[N + 1][M + 1];
        for(int i = N - 1;i >= 0;i--){
            for(int j = M - 1;j >= 0;j--){
                int p1 = 0;
                if(str1[i] == str2[j]){
                    p1 =dp[i+1][j+1]+1;
                }
                int p2 = dp[i+1][j];
                int p3 = dp[i][j+1];
               dp[i][j] = Math.max(p1,Math.max(p2,p3));
            }
        }
        return dp[0][0];
    }
    public static int process(char[] str1,char[] str2,int i,int j){
        if(i == str1.length || j == str2.length){
            return 0;
        }
        int p1 = 0;
        if(str1[i] == str2[j]){
            p1 = process(str1,str2,i+1,j+1)+1;
        }
        int p2 = process(str1,str2,i+1,j);
        int p3 = process(str1,str2,i,j+1);
        return Math.max(p1,Math.max(p2,p3));
    }
}
